package com.umbrella.manage.utils;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

/**
 *
 *
 * @author Wan, ChuanLong @ 09/13/2018
 */
public class ImageUtils {

	public static String getSmallPic(String source, String format, String targetPath, int width, int height, String prefix) {
		
		try {
			
			File imageFile = new File(source);
			
			if(imageFile.exists()) {
				Image image = ImageIO.read(imageFile);
				
//                int sourceWidth = image.getWidth(null);
//                int sourceHeight = image.getHeight(null);
//                
//                if((sourceWidth*1.0)/width < (sourceHeight*1.0)/height){
//                	
//                    if(sourceWidth > width){
//                    	height = Integer.parseInt(new java.text.DecimalFormat("0").format(sourceHeight * width/(sourceWidth*1.0)));
//                    }
//                } else {
//                	
//                    if(sourceHeight > height){
//                    	width = Integer.parseInt(new java.text.DecimalFormat("0").format(sourceWidth * height/(sourceHeight*1.0)));
//                    }
//                }
                
                BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                
                Graphics g = bi.getGraphics();
                g.drawImage(image, 0, 0, width, height, Color.LIGHT_GRAY, null);
                g.dispose();
                
                String targetName = prefix +imageFile.getName();
                ImageIO.write(bi, format, new File(targetPath + targetName));
                
                return targetName;
			}
		}catch(Exception ex) {

		}
		
		return null;
	}
	
	public String getFormat(String fullName) {
		
		String format = fullName.substring(fullName.lastIndexOf(".") + 1);
		return format;
	}
	
	public String getPath(String fullName) {
		
		String targetPath = fullName.substring(0, fullName.lastIndexOf("/") + 1);
		return targetPath;
	}
	
	public static void main(String[] args) {
		
		ImageUtils.getSmallPic("F:/audio-test/APP标100 副本.png", "png", "F:/audio-test/", 150, 150, "small_");
    }
}
